package com.eshop.tools.rest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.tools.domain.vo.EmailVo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import com.eshop.tools.domain.VerificationCode;
import com.eshop.tools.service.EmailConfigService;
import com.eshop.tools.service.VerificationCodeService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({ "/api/code" })
@Api(tags = { "工具：验证码管理" })
public class VerificationCodeController
{
    private final VerificationCodeService verificationCodeService;
    private final EmailConfigService emailService;
    
    public VerificationCodeController(final VerificationCodeService verificationCodeService, final EmailConfigService emailService) {
        this.verificationCodeService = verificationCodeService;
        this.emailService = emailService;
    }
    
    @PostMapping({ "/resetEmail" })
    @ApiOperation("重置邮箱，发送验证码")
    public ResponseEntity<Object> resetEmail(@RequestBody final VerificationCode code) throws Exception {
        code.setScenes("重置邮箱");
        final EmailVo emailVo = this.verificationCodeService.sendEmail(code);
        this.emailService.send(emailVo, this.emailService.find());
        return (ResponseEntity<Object>)new ResponseEntity(HttpStatus.OK);
    }
    
    @PostMapping({ "/email/resetPass" })
    @ApiOperation("重置密码，发送验证码")
    public ResponseEntity<Object> resetPass(@RequestParam final String email) throws Exception {
        final VerificationCode code = new VerificationCode();
        code.setType("email");
        code.setValue(email);
        code.setScenes("重置邮箱");
        final EmailVo emailVo = this.verificationCodeService.sendEmail(code);
        this.emailService.send(emailVo, this.emailService.find());
        return (ResponseEntity<Object>)new ResponseEntity(HttpStatus.OK);
    }
    
    @GetMapping({ "/validated" })
    @ApiOperation("验证码验证")
    public ResponseEntity<Object> validated(final VerificationCode code) {
        this.verificationCodeService.validated(code);
        return (ResponseEntity<Object>)new ResponseEntity(HttpStatus.OK);
    }
}
